//
//  FSDepAdressViewController.m
//  Trade
//
//  Created by 刘沿 on 16/4/14.
//  Copyright © 2016年 刘沿. All rights reserved.
//

#import "FSDepAdressViewController.h"

@interface DepAddressCellList : UITableViewCell

@property (weak, nonatomic) IBOutlet UILabel *subject;

@property (weak, nonatomic) IBOutlet UILabel *des;


@end

@implementation DepAddressCellList



@end



@interface FSDepAdressViewController ()

@end

@implementation FSDepAdressViewController

-(NSMutableArray*)contents{
    if (_contents == nil) {
        _contents = [NSMutableArray array];
    }
    return _contents;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    
    if (self.contents.count == 0) {
        [self requestData];
    }
    [self setRightItem];
    [self setLeftItem];
    
}
-(void)setRightItem{
    UIBarButtonItem * rightitem = [[UIBarButtonItem alloc] initWithTitle:@"搜索" style:UIBarButtonItemStylePlain target:self action:@selector(searchAction)];
    self.navigationItem.rightBarButtonItem = rightitem;
}

- (void)setLeftItem{
    UIBarButtonItem *leftItem = [[UIBarButtonItem alloc] init];
    
    leftItem.title = @"返回";
    
    self.navigationItem.backBarButtonItem = leftItem;
}

-(void)searchAction{
    
    UIAlertView * alertView = [[UIAlertView alloc] initWithTitle:@"搜索通讯录" message:nil delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
    alertView.delegate = self;
    alertView.alertViewStyle = UIAlertViewStylePlainTextInput;
    UITextField * textField = [alertView textFieldAtIndex:0];
    textField.placeholder = @"请输入关键字";
    [alertView show];
}
//搜素通讯录
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
    
    if (buttonIndex == 1) {
        UITextField * textField = [alertView textFieldAtIndex:0];
        if (textField.text.length == 0) {
            return;
        }
        [MBProgressHUD showHUDAddedTo:self.navigationController.view animated:YES];
        NSString * userId = [[NSUserDefaults standardUserDefaults]objectForKey:@"userId"];
        NSArray *  array = @[@{@"condition":textField.text},@{@"userId":userId},@{@"page":@"1"}];;
        [HttpTools requestWithURL:KExchother arrays:array method:KAdressListSearch success:^(id JSON) {
            [MBProgressHUD hideAllHUDsForView:self.navigationController.view animated:YES];
            NSArray * tempArray = JSON[@"data"];
            NSDictionary *dict = [tempArray firstObject];
                        
            if (dict) {
                NSArray * array = @[dict];
                self.contents = [array mutableCopy];
            }else {
                [self.view makeToast:@"搜索无相关数据"];

            }

//            if (self.contents.count == 0) {
//                [self.view makeToast:@"搜索无相关数据"];
//            }
            
            [self.tableView reloadData];
            
            
        } failure:^(NSError *error) {
            [MBProgressHUD hideAllHUDsForView:self.navigationController.view animated:YES];
        }];
    }
}


-(void)requestData{
    [MBProgressHUD showHUDAddedTo:self.navigationController.view animated:YES];
    NSArray *  array = @[@{@"path":self.path}];
    [HttpTools requestWithURL:KExchother arrays:array method:KDepTxlTree success:^(id JSON) {
        [MBProgressHUD hideAllHUDsForView:self.navigationController.view animated:YES];
        NSArray * tempArray = JSON[@"data"];
        NSDictionary *dict = [tempArray firstObject];
        self.contents = dict[@"PartNode"];
        [self.tableView reloadData];
    } failure:^(NSError *error) {
        [MBProgressHUD hideAllHUDsForView:self.navigationController.view animated:YES];
    }];
    

    
}

-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    DepAddressCellList * cell = [tableView dequeueReusableCellWithIdentifier:@"DepCellId" forIndexPath:indexPath];
    NSDictionary *dict = self.contents[indexPath.row];
    cell.subject.text = dict[@"ryxm"];
    cell.des.text = dict[@"duties"];
    return cell;
}

//-(UIColor *) randomColor
//{
//    CGFloat hue = ( arc4random() % 256 / 256.0 ); //0.0 to 1.0
//    CGFloat saturation = ( arc4random() % 128 / 256.0 ) + 0.5; // 0.5 to 1.0,away from white
//    CGFloat brightness = ( arc4random() % 128 / 256.0 ) + 0.5; //0.5 to 1.0,away from black
//    return [UIColor colorWithHue:hue saturation:saturation brightness:brightness alpha:1];
//    
//}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.contents.count;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 56;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    UIStoryboard * storyBoard = [UIStoryboard storyboardWithName:@"MaoCuHui" bundle:nil];
    FSDressDetailViewController * detailVC = (FSDressDetailViewController*)[storyBoard instantiateViewControllerWithIdentifier:@"DressDetailVC"];
    NSDictionary *dict = self.contents[indexPath.row];
    detailVC.unid = dict[@"unid"];
    [self.navigationController pushViewController:detailVC animated:YES];
}

@end
